﻿namespace ExamV5.PaperInfo
{
    using ExamV5;
    using System;
    using System.Configuration;
    using System.Data;
    using System.Data.SqlClient;
    using System.Drawing;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;

    public class FrmManagPaperList : Page
    {
        protected Button butQuery;
        protected GridView DataGridInfo;
        protected DropDownList DDLDiff;
        protected DropDownList DDLQuery;
        protected DropDownList DDLRubricFlag;
        protected DropDownList DDLSubject;
        protected HtmlForm Form1;
        protected HtmlHead Head1;
        private HttpCookie httpCookie_0 = null;
        private HttpCookie httpCookie_1 = null;
        private HttpCookie httpCookie_2 = null;
        private HttpCookie httpCookie_3 = null;
        protected Label labRescordCount;
        protected LinkButton LBCheck;
        protected LinkButton LBDel;
        protected LinkButton LBDelAll;
        protected LinkButton LBFirstPage;
        protected LinkButton LBLastPage;
        protected LinkButton LBNextPage;
        protected LinkButton LBPrevPage;
        protected LinkButton LBSelect;
        private PublicFun publicFun_0 = new PublicFun();
        protected ScriptManager SM;
        protected TextBox txtEndDate;
        protected TextBox txtQuery;
        protected TextBox txtStartDate;
        protected UpdatePanel UPPart;

        protected void butQuery_Click(object sender, EventArgs e)
        {
            this.DataGridInfo.PageIndex = 0;
            this.method_0();
        }

        protected void DataGridInfo_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.DataGridInfo.PageIndex = e.NewPageIndex;
            this.method_0();
        }

        protected void DataGridInfo_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.DataItemIndex == -1)
            {
                this.DataGridInfo.Attributes.Add("oldValue", "#FFFFFF");
                this.DataGridInfo.Attributes.Add("singleValue", "#FFFFFF");
            }
            else
            {
                e.Row.Attributes.Add("onmouseover", "this.bgColor='#F6F5F5'");
                if ((e.Row.DataItemIndex % 2) == 0)
                {
                    e.Row.Attributes.Add("bgcolor", "#FFFFFF");
                    e.Row.Attributes.Add("onmouseout", "this.bgColor=document.getElementById('DataGridInfo').getAttribute('singleValue')");
                }
                else
                {
                    e.Row.Attributes.Add("bgcolor", "#FFFFFF");
                    e.Row.Attributes.Add("onmouseout", "this.bgColor=document.getElementById('DataGridInfo').getAttribute('oldValue')");
                }
            }
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if (e.Row.Cells[1].Text.Length > Convert.ToInt32(this.ViewState["SubStrLen"].ToString()))
                {
                    if (!(this.DataGridInfo.DataKeys[e.Row.RowIndex]["IsCheck"].ToString() != "N"))
                    {
                        e.Row.Cells[1].Text = "<a title='" + e.Row.Cells[1].Text + "'>" + e.Row.Cells[1].Text.Substring(0, Convert.ToInt32(this.ViewState["SubStrLen"].ToString())) + "…</a>";
                    }
                    else
                    {
                        e.Row.Cells[1].Text = "<a title='此试卷需要审核：" + e.Row.Cells[1].Text + "'><font color=red>" + e.Row.Cells[1].Text.Substring(0, Convert.ToInt32(this.ViewState["SubStrLen"].ToString())) + "…</font></a>";
                    }
                }
                else if (this.DataGridInfo.DataKeys[e.Row.RowIndex]["IsCheck"].ToString() != "N")
                {
                    e.Row.Cells[1].Text = "<a title='此试卷需要审核'><font color=red>" + e.Row.Cells[1].Text + "</font></a>";
                }
                if (e.Row.Cells[2].Text.Length > 10)
                {
                    e.Row.Cells[2].Text = "<a title=" + e.Row.Cells[2].Text + ">" + e.Row.Cells[2].Text.Substring(0, 10) + "…</a>";
                }
                Label label6 = (Label) e.Row.Cells[6].FindControl("labPrintPaper");
                Label label7 = (Label) e.Row.Cells[6].FindControl("labPrintAnswer");
                label6.Text = "<a href='PrintPaperInfo.aspx?ID=" + this.DataGridInfo.DataKeys[e.Row.RowIndex]["ID"].ToString() + "' target='_blank'>试卷</a>";
                label7.Text = "<a href='PrintAnswerInfo.aspx?ID=" + this.DataGridInfo.DataKeys[e.Row.RowIndex]["ID"].ToString() + "' target='_blank'>答案</a>";
                Label label5 = (Label) e.Row.Cells[7].FindControl("labLastExam");
                Label label = (Label) e.Row.Cells[7].FindControl("labLook");
                Label label2 = (Label) e.Row.Cells[7].FindControl("labActive");
                Label label4 = (Label) e.Row.Cells[7].FindControl("labEdit");
                Label label3 = (Label) e.Row.Cells[7].FindControl("labAddUser");
                if (!(this.publicFun_0.GetValues("select UserCode from ExamUser where Flag<>'A' and PaperID=" + this.DataGridInfo.DataKeys[e.Row.RowIndex]["ID"].ToString(), "UserCode") != ""))
                {
                    label4.Text = "<a href='RndAddPaper.aspx?ID=" + this.DataGridInfo.DataKeys[e.Row.RowIndex]["ID"].ToString() + "'>" + label4.Text + "</a>";
                    label5.Enabled = false;
                    label5.Attributes.Add("title", "还未开考,无法设置");
                    label5.ForeColor = Color.Gray;
                }
                else
                {
                    label4.Text = "<a href='#' onclick=javascript:EditPaper('" + this.DataGridInfo.DataKeys[e.Row.RowIndex]["ID"].ToString() + "');>" + label4.Text + "</a>";
                    label5.Text = "<a href='#' onclick=javascript:SetLastExam('" + this.DataGridInfo.DataKeys[e.Row.RowIndex]["ID"].ToString() + "');>" + label5.Text + "</a>";
                }
                DateTime time = Convert.ToDateTime(this.DataGridInfo.DataKeys[e.Row.RowIndex]["PaperEndTime"].ToString());
                if (DateTime.Now > time)
                {
                    label3.ForeColor = Color.Gray;
                    label3.Enabled = false;
                    label3.Text = "<a title='试卷已过期,试卷有效期：" + this.DataGridInfo.DataKeys[e.Row.RowIndex]["PaperStartTime"].ToString() + "--" + this.DataGridInfo.DataKeys[e.Row.RowIndex]["PaperEndTime"].ToString() + "'>" + label3.Text + "</a>";
                }
                if (label3.Enabled)
                {
                    label3.Text = "<a href='#' title='补录参加考试人员' onclick=javascript:AddJoinExamUser('" + this.DataGridInfo.DataKeys[e.Row.RowIndex]["ID"].ToString() + "');><font color=#ff6600>" + label3.Text + "</font></a>";
                }
                label.Text = "<a href='#' onclick=javascript:ShowPaperInfo('" + this.DataGridInfo.DataKeys[e.Row.RowIndex]["ID"].ToString() + "');>" + label.Text + "</a>";
                label2.Text = "<a href='UserAddPaper.aspx?ID=" + this.DataGridInfo.DataKeys[e.Row.RowIndex]["ID"].ToString() + "'>" + label2.Text + "</a>";
            }
            if (e.Row.RowType == DataControlRowType.Header)
            {
                if (this.ViewState["SortType"].ToString() == " Desc")
                {
                    switch (this.ViewState["SortFiled"].ToString())
                    {
                        case "ExamName":
                            e.Row.Cells[1].Controls.Add(new LiteralControl("↑"));
                            return;

                        case "SubjectName":
                            e.Row.Cells[2].Controls.Add(new LiteralControl("↑"));
                            return;

                        case "PaperFen":
                            e.Row.Cells[3].Controls.Add(new LiteralControl("↑"));
                            return;

                        case "ExamTime":
                            e.Row.Cells[4].Controls.Add(new LiteralControl("↑"));
                            return;

                        case "PaperStatus":
                            e.Row.Cells[5].Controls.Add(new LiteralControl("↑"));
                            return;
                    }
                    e.Row.Cells[1].Controls.Add(new LiteralControl("↑"));
                }
                else
                {
                    switch (this.ViewState["SortFiled"].ToString())
                    {
                        case "ExamName":
                            e.Row.Cells[1].Controls.Add(new LiteralControl("↓"));
                            return;

                        case "SubjectName":
                            e.Row.Cells[2].Controls.Add(new LiteralControl("↓"));
                            return;

                        case "PaperFen":
                            e.Row.Cells[3].Controls.Add(new LiteralControl("↓"));
                            return;

                        case "ExamTime":
                            e.Row.Cells[4].Controls.Add(new LiteralControl("↓"));
                            return;

                        case "PaperStatus":
                            e.Row.Cells[5].Controls.Add(new LiteralControl("↓"));
                            return;
                    }
                    e.Row.Cells[1].Controls.Add(new LiteralControl("↓"));
                }
            }
        }

        protected void DataGridInfo_Sorting(object sender, GridViewSortEventArgs e)
        {
            string sortExpression = e.SortExpression;
            if (this.ViewState["SortFiled"].ToString() == sortExpression)
            {
                if (!(this.ViewState["SortType"].ToString() == " Desc"))
                {
                    this.ViewState["SortType"] = " Desc";
                }
                else
                {
                    this.ViewState["SortType"] = " ASC";
                }
            }
            else
            {
                this.ViewState["SortFiled"] = e.SortExpression;
            }
            this.method_0();
        }

        protected void DDLDiff_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.DataGridInfo.PageIndex = 0;
            this.method_0();
        }

        protected void DDLRubricFlag_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.DataGridInfo.PageIndex = 0;
            this.method_0();
        }

        protected void DDLSubject_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.DataGridInfo.PageIndex = 0;
            this.method_0();
        }

        protected void LBCheck_Click(object sender, EventArgs e)
        {
            string str = "";
            int num = 0;
            SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Conn"].ConnectionString);
            connection.Open();
            SqlCommand command = null;
            for (int i = 0; i < this.DataGridInfo.Rows.Count; i++)
            {
                CheckBox box = (CheckBox) this.DataGridInfo.Rows[i].FindControl("CheckBoxSelect");
                if (box.Checked)
                {
                    num++;
                    str = "Update PaperInfo set IsCheck='N' where ID=" + this.DataGridInfo.DataKeys[i]["ID"].ToString();
                    command = new SqlCommand(str + ";delete from SysTip where Formtable='PaperInfo'  and FormID=" + this.DataGridInfo.DataKeys[i]["ID"].ToString() + ";insert into SysLogInfo(LogDisp,EditUser,EditUserCode) values('审核考试试卷[" + this.DataGridInfo.DataKeys[i]["ExamName"].ToString() + "]信息！','" + this.httpCookie_1.Value + "','" + this.httpCookie_0.Value + "')", connection);
                    command.ExecuteNonQuery();
                    command.Dispose();
                }
            }
            connection.Close();
            connection.Dispose();
            if (this.DataGridInfo.Rows.Count == num)
            {
                this.DataGridInfo.PageIndex = 0;
            }
            this.method_0();
            this.LBSelect.Text = "选中当页";
        }

        protected void LBDel_Click(object sender, EventArgs e)
        {
            string str = "";
            int num = 0;
            SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Conn"].ConnectionString);
            connection.Open();
            SqlCommand command = null;
            for (int i = 0; i < this.DataGridInfo.Rows.Count; i++)
            {
                CheckBox box = (CheckBox) this.DataGridInfo.Rows[i].FindControl("CheckBoxSelect");
                if (box.Checked)
                {
                    num++;
                    str = (((((("delete from  PaperInfo where ID=" + this.DataGridInfo.DataKeys[i]["ID"].ToString()) + ";delete from PaperData where PaperID=" + this.DataGridInfo.DataKeys[i]["ID"].ToString()) + ";delete from PaperDataTmp where PaperID=" + this.DataGridInfo.DataKeys[i]["ID"].ToString()) + ";delete from ExamUser where PaperID=" + this.DataGridInfo.DataKeys[i]["ID"].ToString()) + ";delete from CheckPaper where  PaperID=" + this.DataGridInfo.DataKeys[i]["ID"].ToString()) + ";delete from GradeReule where  PaperID=" + this.DataGridInfo.DataKeys[i]["ID"].ToString()) + ";delete from PaperRule where  PaperID=" + this.DataGridInfo.DataKeys[i]["ID"].ToString();
                    command = new SqlCommand(str + ";delete from SysTip where Formtable='PaperInfo'  and FormID=" + this.DataGridInfo.DataKeys[i]["ID"].ToString() + ";insert into SysLogInfo(LogDisp,EditUser,EditUserCode) values('删除考试试卷[" + this.DataGridInfo.DataKeys[i]["ExamName"].ToString() + "]信息！','" + this.httpCookie_1.Value + "','" + this.httpCookie_0.Value + "')", connection);
                    command.ExecuteNonQuery();
                    command.Dispose();
                }
            }
            connection.Close();
            connection.Dispose();
            if (this.DataGridInfo.Rows.Count == num)
            {
                this.DataGridInfo.PageIndex = 0;
            }
            this.method_0();
            this.LBSelect.Text = "选中当页";
        }

        protected void LBDelAll_Click(object sender, EventArgs e)
        {
            string strSql = "";
            strSql = "delete from  PaperInfo ";
            strSql = (((strSql + ";delete from PaperData ") + ";delete from PaperDataTmp " + ";delete from ExamUser ") + ";delete from CheckPaper " + ";delete from GradeReule where PaperID<>0") + ";delete from PaperRule " + ";delete from SysTip where Formtable='PaperInfo' ";
            strSql = strSql + ";insert into SysLogInfo(LogDisp,EditUser,EditUserCode) values('清除所有考试试卷信息！','" + this.httpCookie_1.Value + "','" + this.httpCookie_0.Value + "')";
            this.publicFun_0.ExecuteSql(strSql);
            this.DataGridInfo.PageIndex = 0;
            this.method_0();
            this.LBSelect.Text = "选中当页";
        }

        protected void LBFirstPage_Click(object sender, EventArgs e)
        {
            this.DataGridInfo.PageIndex = 0;
            this.method_0();
        }

        protected void LBLastPage_Click(object sender, EventArgs e)
        {
            if (this.DataGridInfo.PageCount > 0)
            {
                this.DataGridInfo.PageIndex = this.DataGridInfo.PageCount - 1;
                this.method_0();
            }
        }

        protected void LBNextPage_Click(object sender, EventArgs e)
        {
            if (this.DataGridInfo.PageIndex < (this.DataGridInfo.PageCount - 1))
            {
                this.DataGridInfo.PageIndex++;
                this.method_0();
            }
        }

        protected void LBPrevPage_Click(object sender, EventArgs e)
        {
            if (this.DataGridInfo.PageIndex > 0)
            {
                this.DataGridInfo.PageIndex--;
                this.method_0();
            }
        }

        protected void LBSelect_Click(object sender, EventArgs e)
        {
            int num;
            CheckBox box;
            if (this.LBSelect.Text != "选中当页")
            {
                for (num = 0; num < this.DataGridInfo.Rows.Count; num++)
                {
                    box = (CheckBox) this.DataGridInfo.Rows[num].FindControl("CheckBoxSelect");
                    box.Checked = false;
                }
                this.LBSelect.Text = "选中当页";
            }
            else
            {
                for (num = 0; num < this.DataGridInfo.Rows.Count; num++)
                {
                    box = (CheckBox) this.DataGridInfo.Rows[num].FindControl("CheckBoxSelect");
                    if (box.Enabled)
                    {
                        box.Checked = true;
                    }
                }
                this.LBSelect.Text = "取消所选";
            }
        }

        private void method_0()
        {
            string selectCommandText = "";
            selectCommandText = "select IsCheck,PaperStartTime,PaperEndTime,ID,ExamName,SubjectName,PaperFen,InputMan,PaperDiff=case PaperDiff when 'A' then '难' when 'B' then '较难'  when 'C' then '中等' when 'D' then '较易' when 'E' then '容易' end,ExamTime,PaperStatus=case PaperStatus when 'A' then '正规' when 'C' then '模拟' end from PaperInfo  where  ID in (select DISTINCT PaperID from ExamUser) ";
            if (this.ViewState["ManagPower"].ToString() == "Y")
            {
                selectCommandText = selectCommandText + " and ID<>0 ";
            }
            else if (this.ViewState["AddPower"].ToString() != "Y")
            {
                if (!(this.ViewState["CheckPower"].ToString() == "Y"))
                {
                    selectCommandText = selectCommandText + " and ID=0 ";
                    ScriptManager.RegisterStartupScript(this.UPPart, this.Page.GetType(), "BPower", "alert('组卷或管理试卷权限未开放！')", true);
                }
                else
                {
                    selectCommandText = selectCommandText + " and CheckMan='" + this.httpCookie_0.Value + "'";
                }
            }
            else
            {
                selectCommandText = selectCommandText + " and InputUserCode='" + this.httpCookie_0.Value + "'";
            }
            if (this.DDLSubject.SelectedItem.Value != "0")
            {
                selectCommandText = selectCommandText + " and SubjectName like '%" + this.publicFun_0.CheckString(this.DDLSubject.SelectedItem.Text.Replace("│", "").Replace("├", "")) + "%'";
            }
            if (this.DDLRubricFlag.SelectedItem.Value != "0")
            {
                selectCommandText = selectCommandText + " and PaperStatus='" + this.publicFun_0.CheckString(this.DDLRubricFlag.SelectedItem.Value) + "'";
            }
            if (this.DDLDiff.SelectedItem.Value != "0")
            {
                selectCommandText = selectCommandText + " and PaperDiff='" + this.publicFun_0.CheckString(this.DDLDiff.SelectedItem.Value) + "'";
            }
            if (this.txtQuery.Text.Trim() != "")
            {
                selectCommandText = selectCommandText + " and " + this.publicFun_0.CheckString(this.DDLQuery.SelectedItem.Value) + " like '%" + this.publicFun_0.CheckString(this.txtQuery.Text) + "%'";
            }
            if ((this.txtStartDate.Text != "") && (this.txtEndDate.Text != ""))
            {
                selectCommandText = selectCommandText + " and (InputTime>=Convert(DateTime,'" + this.publicFun_0.CheckString(this.txtStartDate.Text) + "') and InputTime<=Convert(dateTime,'" + this.publicFun_0.CheckString(this.txtEndDate.Text) + "'))";
            }
            selectCommandText = selectCommandText + " order by " + this.ViewState["SortFiled"].ToString() + this.ViewState["SortType"].ToString();
            SqlConnection selectConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["Conn"].ConnectionString);
            SqlDataAdapter adapter = new SqlDataAdapter(selectCommandText, selectConnection);
            DataSet dataSet = new DataSet();
            adapter.Fill(dataSet, "DataInfo");
            this.DataGridInfo.DataSource = dataSet.Tables["DataInfo"].DefaultView;
            this.DataGridInfo.DataBind();
            this.labRescordCount.Text = "总共&nbsp;<font color='#0000C0'><b>" + dataSet.Tables["DataInfo"].Rows.Count.ToString() + "</b></font>&nbsp;条记录&nbsp;<font color='#0000C0'><b>" + Convert.ToString((int) (this.DataGridInfo.PageIndex + 1)) + "/" + this.DataGridInfo.PageCount.ToString() + "</b></font>&nbsp;页&nbsp;";
            adapter.Dispose();
            selectConnection.Close();
            selectConnection.Dispose();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.httpCookie_0 = new HttpCookie("ExamUserCode");
            this.httpCookie_0 = base.Request.Cookies["ExamUserCode"];
            this.httpCookie_1 = new HttpCookie("ExamUserName");
            this.httpCookie_1 = base.Request.Cookies["ExamUserName"];
            this.httpCookie_2 = new HttpCookie("ExamUserLevel");
            this.httpCookie_2 = base.Request.Cookies["ExamUserLevel"];
            this.httpCookie_3 = new HttpCookie("ExamDeptID");
            this.httpCookie_3 = base.Request.Cookies["ExamDeptID"];
            if ((this.httpCookie_0 != null) ? !(this.httpCookie_0.Value == "") : false)
            {
                base.Response.Cache.SetCacheability(HttpCacheability.NoCache);
                base.Response.Cache.SetNoStore();
                this.httpCookie_0.Value = this.publicFun_0.CheckString(base.Server.UrlDecode(this.httpCookie_0.Value));
                this.httpCookie_1.Value = this.publicFun_0.CheckString(base.Server.UrlDecode(this.httpCookie_1.Value));
                this.httpCookie_2.Value = this.publicFun_0.CheckString(base.Server.UrlDecode(this.httpCookie_2.Value));
                this.httpCookie_3.Value = this.publicFun_0.CheckString(base.Server.UrlDecode(this.httpCookie_3.Value));
                if (!base.IsPostBack)
                {
                    if (this.publicFun_0.ReturnPaperData() != "Yes")
                    {
                        ScriptManager.RegisterStartupScript(this.UPPart, this.Page.GetType(), "BCheck", "alert('此版本暂未授权,请拔打(0)13870606104手机号进行授权！')", true);
                    }
                    else
                    {
                        this.txtQuery.Attributes.Add("onkeydown", "SubmitKeyClick('butQuery');");
                        SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Conn"].ConnectionString);
                        connection.Open();
                        SqlCommand command = new SqlCommand("select AddPaper,CheckPaper,ManagPaper from UserPower where UserCode='" + this.httpCookie_0.Value + "'", connection);
                        SqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection);
                        if (reader.Read())
                        {
                            this.ViewState["AddPower"] = reader["AddPaper"].ToString();
                            this.ViewState["CheckPower"] = reader["CheckPaper"].ToString();
                            this.ViewState["ManagPower"] = reader["ManagPaper"].ToString();
                        }
                        reader.Close();
                        command.Dispose();
                        connection.Close();
                        connection.Dispose();
                        this.ViewState["SortFiled"] = "ID";
                        this.ViewState["SortType"] = " Desc";
                        if (this.ViewState["CheckPower"].ToString() == "Y")
                        {
                            this.LBCheck.Enabled = true;
                            this.LBCheck.Attributes.Add("onclick", "javascript:{if(confirm('确定审核通过所有选中信息吗？')==false) return false;}");
                        }
                        else
                        {
                            this.LBCheck.Enabled = false;
                        }
                        this.LBDel.Attributes.Add("onclick", @"javascript:{if(confirm('删除试卷后相关考生成绩也将连带删除\n\n请再次确认您是否删除所选试卷？')==false) return false;}");
                        if (!(this.ViewState["ManagPower"].ToString() == "Y"))
                        {
                            this.LBDelAll.Enabled = false;
                        }
                        else
                        {
                            this.LBDelAll.Enabled = true;
                            this.LBDelAll.Attributes.Add("onclick", @"javascript:{if(confirm('您当前删除是清除所有试卷\n\n清除所有试卷后所有考生成绩也全部清除\n\n请再次确认您是否清除所有试卷？')==false) return false;}");
                        }
                        this.publicFun_0.LoadSubjectInfo(this.DDLSubject, this.httpCookie_0.Value, this.ViewState["ManagPower"].ToString(), "B", this.httpCookie_3.Value);
                        this.DDLSubject.Items.Insert(0, new ListItem("--请选择所属科目信息--", "0"));
                        string str = this.publicFun_0.GetValues("select ParmValue from SysParmInfo where ParmMemo='SubStrLength'", "ParmValue").Trim();
                        if (str.Trim() == "")
                        {
                            str = "20";
                        }
                        this.ViewState["SubStrLen"] = str;
                        this.method_0();
                    }
                }
            }
            else
            {
                base.Response.Write("<script language=javascript>top.location.href='../Index.htm';self.close();</script>");
            }
        }
    }
}

